//
//  YQRefreshHeadView.m
//  first
//
//  Created by 毕小强 on 16/11/9.
//  Copyright © 2016年 毕小强. All rights reserved.
//

#import "YQRefreshHeadView.h"
#import <objc/message.h>
@implementation YQRefreshHeadView
-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.stateLab = [[UILabel alloc]initWithFrame:CGRectMake(0, kStartRefreshHeight, [UIScreen mainScreen].bounds.size.width, kStartRefreshHeight)];
        self.stateLab.textAlignment = NSTextAlignmentCenter;
        self.stateLab.text = @"下拉刷新";
        [self addSubview:self.stateLab];
    }
    return self;
}
-(void)willMoveToSuperview:(UIView *)newSuperview{
    [super willMoveToSuperview:newSuperview];
    self.superScrollView = (UIScrollView*)newSuperview;
    [newSuperview addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    CGFloat offY = self.superScrollView.contentOffset.y;
    self.superScrollViewContOffet = offY;
    
    YQRefreshState newState;
    if (self.headerRefreshState == YQRefreshingState) {
        newState = YQRefreshingState;
    }else{
        if (offY <= -kStartRefreshHeight) {
            if (self.superScrollView.isDragging) {
                newState = YQRefreshWillState;
            }else{
                newState = YQRefreshingState;
            }
        }else{
            newState = YQRefreshNormalState;
        }
    }
    
    if (self.headerRefreshState != newState) {
        self.headerRefreshState = newState;
    }
}
-(void)setHeaderRefreshState:(YQRefreshState)headerRefreshState{
    [super setHeaderRefreshState:headerRefreshState];
    switch (headerRefreshState) {
        case YQRefreshNormalState:{
            self.stateLab.text = @"下拉刷新";
            break;
        }
        case YQRefreshingState:{
            self.stateLab.text = @"正在刷新";
            UIEdgeInsets  insert =self.superScrollView.contentInset;
            [UIView animateWithDuration:0.2 animations:^{
                [self.superScrollView setContentInset:UIEdgeInsetsMake(kStartRefreshHeight, 0, insert.bottom, 0)];
            }];
            //objc_msgSend(self.selecterTarget,self.refreshSelector);
            break;
        }
        case YQRefreshWillState:{
            self.stateLab.text = @"松开刷新";
            break;
        }
        default:
            break;
    }
}
-(void)beginRefresh{
    [self.superScrollView setContentOffset:CGPointMake(0, -kStartRefreshHeight)];
    self.headerRefreshState = YQRefreshingState;
}
-(void)endRefresh{
    [UIView animateWithDuration:0.2 animations:^{
        UIEdgeInsets  insert =self.superScrollView.contentInset;
        [self.superScrollView setContentInset:UIEdgeInsetsMake(0, 0, insert.bottom, 0)];
    }];
    self.headerRefreshState = YQRefreshNormalState;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
